<?php
	class Beranda extends CI_Controller{
		function __construct(){
			parent::__construct();
			$this->load->model(array('user','article', 'slider', 'photo', 'video'));
			$this->load->library('session');
			$this->load->helper(array('dharma_date_helper', 'dharma_video_helper'));
			$this->userdata = $this->session->userdata('userdata');
			if($this->userdata){
				$recent_articles = $this->article->get_article(NULL, NULL, 5);
			}else{
				$recent_articles = $this->article->get_article(NULL, NULL, 5, 0, 'public');
			}
			$this->footer = array('categories' => unserialize(ARTICLE_CATEGORY),
								  'recent_articles' => $recent_articles);
		}
		
		public function index(){
			$data['sliders'] = $this->slider->get_slider();
			if($this->userdata){
				$data['articles'] = $this->article->get_article(NULL, NULL, 4);
			}else{
				$data['articles'] = $this->article->get_article(NULL, NULL, 4, 0, 'public');
			}
			$data['photos'] = $this->photo->get_foto(NULL, NULL, 4);
			$data['videos'] = $this->video->get_video(NULL, NULL, 4);
			$data['footer'] = $this->footer;
			$this->load->view('beranda_view', $data);
		}
		
		public function login(){
			$username = $this->input->post('username');
			$password = $this->input->post('password');

			if ($username || $password) {
				if ($user = $this->user->login($username, $password)) {
					$this->session->set_userdata(
						array(
							'userdata' => $user
						)
					);
					echo json_encode('success');					
					//redirect(site_url('beranda'));
					//print_r($this->session->userdata('userdata')); die;
				} else {
					$this->session->set_flashdata('login_msg', 'Incorrect username and/or password');
					echo json_encode('error');
				}
			} else {
				//$this->load->view('beranda/');
				//echo "gagal 1";
			}
		}
		
		public function logout(){
			if ($userdata = $this->session->userdata('userdata')) {
				$this->session->unset_userdata('userdata');
			}
			redirect(site_url('beranda/'));
		}
		
		public function register(){
			if(isset($_POST['registration_submit'])){
				$post_data_user = array('username' => $this->input->post('username'),
									    'password' => md5($this->input->post('password')),
									    'level'	   => ALUMNI_TYPE
				);
				$id = $this->user->add($post_data_user);
				$post_data_alumni = array('id_user'		=> $id,
										  'nama' 		=> $this->input->post('nama'),
										  'pekerjaan'	=> $this->input->post('pekerjaan'),
										  'alamat' 		=> $this->input->post('alamat'),
										  'telepon'		=> $this->input->post('telepon'),
										  'email'		=> $this->input->post('email'),
										  'hp'			=> $this->input->post('hp'),
										  'status'		=> $this->input->post('status'),
										  'jk'			=> $this->input->post('jk')
				);
				$alumni = $this->user->add_alumni($post_data_alumni);
				copy("./images/user_avatars/default.jpg", "./images/user_avatars/" . $id . ".jpg");
				if($id){
					$this->session->set_flashdata('registration_success', array('success' => 'true', 'message' => 'Registrasi dengan username <b>' .$post_data_user['username']. '</b> berhasil dilakukan, untuk mengaktifkan account anda agar bisa login, silahkan hubungi administrator.'));
				}else{
					$this->session->set_flashdata('registration_success', array('success' => 'false', 'message' => 'Maaf registrasi yang anda lakukan gagal, silahkan hubungi administrator atau mencoba kembali.'));
				}
				redirect('beranda/register');
			}
			$data['registration_success_msg'] = $this->session->flashdata('registration_success');
			$data['footer'] = $this->footer;
			$this->load->view('user/register', $data);
		}
}
?>